package com.zjuee.gateway.utils;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;

/**
 * @author wu liang
 * @since 2020/8/13 9:54
 */
public class HttpRequestUtils {

    /**
     * 从request 中获取body
     */
    public static Map<String, Object> getBody(ServletRequest request) {
        Map<String, Object> map = null;
        try (BufferedReader br = request.getReader()) {
            StringBuilder sb = new StringBuilder("");
            String str;
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            if (sb.length() > 1)
                map = JSONObject.parseObject(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
        }
        return map;
    }
}
